#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time : 2025/7/23 22:24
# @Author : liuqi
# @File : baidu_page.py
# @Software: PyCharm
# @Describe:
from playwright.sync_api import Page

class BaiduPage:
    def __init__(self, page: Page):
        self.page = page
        self.search_input = "#kw"
        self.search_button = "#su"
        self.first_result = "//div[@id='content_left']/div[1]//a"

    def navigate(self):
        self.page.goto("https://www.baidu.com")

    def search(self, keyword: str):
        self.page.fill(self.search_input, keyword)
        self.page.click(self.search_button)
        self.page.wait_for_selector(self.first_result)

    def get_first_result_title(self) -> str:
        return self.page.locator(self.first_result).first.text_content()